﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace KZ400B.Controls.TPlan
{
    /// <summary>
    /// TPlanAxle.xaml 的交互逻辑
    /// </summary>
    public partial class TPlanAxle : UserControl
    {
        public TPlanAxle()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(TPlanAxle_Loaded);
        }

        void TPlanAxle_Loaded(object sender, RoutedEventArgs e)
        {
            m_axle = AxleCacheManager.getAxle(AxleName);
            chkAxle.Content = string.Format("{0}轴", AxleName);
            if (TPlanID == 0)
            {
                IsEnabled = false;
            }
            //else
            //{
            //    IsEnabled = true;
            //}
            bindRunningUnits();
            bind();
        }


        //private bool m_IsEnabled;
        private void chkAxle_Checked(object sender, RoutedEventArgs e)
        {
            IsEnabled = true;
            //txtStartPosition.IsEnabled = true;
            //cmbRunUnit.IsEnabled = true;
            //chkAxle.IsChecked = true;
        }

        private void chkAxle_Unchecked(object sender, RoutedEventArgs e)
        {
            IsEnabled = false;
            //txtStartPosition.IsEnabled = false;
            //cmbRunUnit.IsEnabled = false;
            //chkAxle.IsChecked = false;
        }

        public static DependencyProperty AxleNameProperty = DependencyProperty.Register(
        "AxleName", typeof(string)
        , typeof(TPlanAxle)
        , new PropertyMetadata(string.Empty)
        );

        public string AxleName
        {
            get { return (string)GetValue(AxleNameProperty); }
            set { SetValue(AxleNameProperty, value); }
        }

        public int AxleID
        {
            get { return m_axle != null ? m_axle.ID : 0; }
        }

        public new static DependencyProperty IsEnabledProperty = DependencyProperty.Register(
        "IsEnabled", typeof(bool)
        , typeof(TPlanAxle)
        , new PropertyMetadata(true,new PropertyChangedCallback(OnEnableChange))
        );

        public new bool IsEnabled
        {
            get { return (bool)GetValue(IsEnabledProperty); }
            set { SetValue(IsEnabledProperty, value);
                //m_IsEnabled = value; 
            }
        }

        private static void OnEnableChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            TPlanAxle control = (TPlanAxle)d;
            control.setIsEnabled(bool.Parse( e.NewValue.ToString()) );
        }

        void setIsEnabled(bool isEnabled)
        {
            txtStartPosition.IsEnabled = isEnabled;
            //cmbRunUnit.IsEnabled = isEnabled;
            cmbRunUnit.IsEnabled = false;
            chkAxle.IsChecked = isEnabled;

        }


        public static DependencyProperty IsReadOnlyProperty = DependencyProperty.Register(
        "IsReadOnly", typeof(bool)
        , typeof(TPlanAxle)
        , new PropertyMetadata(false, new PropertyChangedCallback(OnReadOnlyChange))
        );

        public bool IsReadOnly
        {
            get { return (bool)GetValue(IsReadOnlyProperty); }
            set
            {
                SetValue(IsReadOnlyProperty, value);
                //m_IsEnabled = value; 
            }
        }

        private static void OnReadOnlyChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            TPlanAxle control = (TPlanAxle)d;
            control.setIsReadOnly(bool.Parse(e.NewValue.ToString()));
        }

        void setIsReadOnly(bool isReadOnly)
        {
            txtStartPosition.IsReadOnly = isReadOnly;
            cmbRunUnit.IsEnabled = true;//!isReadOnly;
            chkAxle.IsEnabled = !isReadOnly;
        }


        //public static DependencyProperty IsCheckedProperty = DependencyProperty.Register(
        //  "IsChecked", typeof(bool)
        //  , typeof(TPlanAxle)
        //  , new PropertyMetadata(true, new PropertyChangedCallback(OnIsCheckedChange))
        //  );

        //public bool IsChecked
        //{
        //    get { return (bool)GetValue(IsCheckedProperty); }
        //    set
        //    {
        //        SetValue(IsCheckedProperty, value);
        //        //m_IsEnabled = value; 
        //    }
        //}

        //private static void OnIsCheckedChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
        //{
        //    TPlanAxle control = (TPlanAxle)d;
        //    control.chkAxle.IsChecked = control.IsEnabled;
        //}


        public static DependencyProperty TPlanIDProperty = DependencyProperty.Register(
       "TPlanID", typeof(int)
       , typeof(TPlanAxle)
       , new PropertyMetadata(0, new PropertyChangedCallback(OnTPlanIDChange))
       );

        //private int m_TPlanID;
        public int TPlanID
        {
            get { return (int)GetValue(TPlanIDProperty); }
            set { SetValue(TPlanIDProperty, value); 
                //m_TPlanID = value;
            }
        }

        private static void OnTPlanIDChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            TPlanAxle control = (TPlanAxle)d;
            control.bind();
        }


        public static DependencyProperty StartPostionTextProperty = DependencyProperty.Register(
        "StartPostionText", typeof(string)
        , typeof(TPlanAxle)
        , new PropertyMetadata("0", new PropertyChangedCallback(OnStartPostionChange))
        );


        public string StartPostionText
        {
            get { return (string)GetValue(StartPostionTextProperty); }
            set
            {
                SetValue(StartPostionTextProperty, value);
            }
        }

        private static void OnStartPostionChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            TPlanAxle control = (TPlanAxle)d;
            control.txtStartPosition.Text = control.StartPostionText;
        }

        public double StartPostion
        {
            get
            {
                double p = 0;
                if (double.TryParse(txtStartPosition.Text, out p))
                {
                    return p;
                }
                return 0;
            }
        }

        public static DependencyProperty RunningUnitValueProperty = DependencyProperty.Register(
        "RunningUnitValue", typeof(int)
        , typeof(TPlanAxle)
        , new PropertyMetadata(0, new PropertyChangedCallback(OnRunningUnitValueChange))
        );


        public int RunningUnitValue
        {
            get { return (int)GetValue(RunningUnitValueProperty); }
            set
            {
                SetValue(RunningUnitValueProperty, value);
            }
        }

        private static void OnRunningUnitValueChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            TPlanAxle control = (TPlanAxle)d;
            control.cmbRunUnit.SelectedValue = control.RunningUnitValue;
        }

        public RunningUnits RunningUnit
        {
            get {
                int p = 0;

                if (
                    cmbRunUnit.SelectedValue != null &&
                    int.TryParse(cmbRunUnit.SelectedValue.ToString(), out p))
                {
                    return (RunningUnits)p;
                }
                return RunningUnits.Step;
            }
        }

        TPlanManager m_tManager = new TPlanManager();
        Axle m_axle;
        void bind()
        {
            DB.Model.TPlan tPlan = m_tManager.getModel(TPlanID);
            if (m_axle == null)
            {
                m_axle = AxleCacheManager.getAxle(AxleName);
            }
            if (cmbRunUnit.ItemsSource == null)
            {
                bindRunningUnits();
            }
            bool binded = false;
            if (tPlan != null)
            {
                Func<DB.Model.TPlanAxle, bool> cdt = new Func<DB.Model.TPlanAxle, bool>(n => n.AxleName == AxleName);
                if (tPlan.AxleList.Count(cdt) > 0)
                {
                    DB.Model.TPlanAxle max = tPlan.AxleList.First(cdt);
                    IsEnabled = true;
                    
                    this.txtStartPosition.Text = max.StartPosition.ToString();
                    if (m_axle == null || m_axle.RunningUnit == RunningUnits.Step)
                    {
                        this.cmbRunUnit.SelectedIndex = 0;
                    }
                    else
                    {
                        this.cmbRunUnit.SelectedValue = (int)m_axle.RunningUnit;//max.RunUnit;
                    }
                    binded = true;
                }
            }
            if(!binded)
            {
                //IsEnabled = false;
                try
                {
                    this.txtStartPosition.Text = AxleCacheManager.AxleList[AxleName] == null
                                ? string.Empty
                                : AxleCacheManager.AxleList[AxleName].DisplayPosition.ToString(MySetting.PositionDisDeciFormatter)
                                ;
                    //this.cmbRunUnit.SelectedIndex = 0;
                    setIsEnabled(false);
                    //this.txtStartPosition.IsEnabled = false;
                    this.cmbRunUnit.IsEnabled = false;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }

        /// <summary>
        /// 运行单位
        /// </summary>
        private static List<KZ400B.AxleSettings.RunningUnit> s_runningUnits = new List<KZ400B.AxleSettings.RunningUnit>()
        {
                new KZ400B.AxleSettings.RunningUnit((int)RunningUnits.Step,"步数"),
                new KZ400B.AxleSettings.RunningUnit((int)RunningUnits.Millimeter,"毫米"),
                new KZ400B.AxleSettings.RunningUnit((int)RunningUnits.Radian,"度数")
        };

        void bindRunningUnits()
        {
            if (cmbRunUnit.ItemsSource != null)
            {
                return;
            }
            List<KZ400B.AxleSettings.RunningUnit> mList = new List<KZ400B.AxleSettings.RunningUnit>(s_runningUnits);
            PowerShiftStations pssType = PowerShiftStations.NoConnected;
            if (m_axle != null)
            {
                pssType = m_axle.PowerShiftStation;
            }
            cmbRunUnit.IsEnabled = true;
            switch (pssType)
            {
                case PowerShiftStations.NoConnected:
                    mList.Clear();
                    cmbRunUnit.IsEnabled = false;
                    break;
                case PowerShiftStations.Translational:
                    mList.RemoveAt((int)RunningUnits.Radian);
                    break;
                case PowerShiftStations.Whirling:
                    mList.RemoveAt((int)RunningUnits.Millimeter);
                    break;
                default:
                    mList.Clear();
                    cmbRunUnit.IsEnabled = false;
                    break;
            }
            if (mList != null && mList.Count > 0)
            {
                cmbRunUnit.ItemsSource = mList;
                cmbRunUnit.DisplayMemberPath = "Name";
                cmbRunUnit.SelectedValuePath = "ID";

                try
                {
                    int ru = AxleCacheManager.AxleList[AxleName] == null ? 0 : (int)AxleCacheManager.AxleList[AxleName].RunningUnit;
                    if (AxleCacheManager.AxleList[AxleName].RunningUnit != 0)
                    {
                        cmbRunUnit.SelectedValue = ru;
                    }
                    else
                    {
                        cmbRunUnit.SelectedIndex = 0;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
    }
}
